<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文物展览系统 - 注册</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">

    <style>
        html,
        body {
            height: 100%;
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", serif;
        }

        body {
            background-image: url(/assets/images/pic1.jpg); /* 文物相关背景图 */
            background-repeat: no-repeat;
            background-position: center center;
            background-size: cover;
            background-attachment: fixed;
        }

        .register {
            width: 500px;
            /* 将固定高度改为最小高度，避免内容溢出 */
            height: 600px;
            background-color: rgba(255, 255, 255, 0.95);
            box-shadow: 0px 0px 50px #333;
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 50px;
            margin: auto;
            padding: 40px; /* 减少顶部内边距 */
            box-sizing: border-box;
            border-radius: 8px;
        }

        .register h3 {
            font-size: 24px;
            margin-bottom: 30px; /* 减少标题底部间距 */
            text-align: center;
            color: #8C1515; /* 文物色调 */
        }

        .error-message {
            color: #dc3545;
            text-align: center;
            margin-bottom: 20px;
            height: 20px;
        }

        .inputBox {
            position: relative;
            height: 50px;
            display: flex;
            align-items: center;
            margin-bottom: 25px; /* 减少输入框之间的间距 */
        }

        .inputBox i {
            position: absolute;
            left: 15px;
            color: #8C1515;
        }

        .inputBox input {
            height: 45px;
            border: 1px solid #ddd;
            flex-grow: 1;
            margin: 0 10px;
            padding-left: 45px; /* 给图标留出空间 */
            border-radius: 4px;
            font-size: 16px;
            outline: none;
        }

        .inputBox input:focus {
            border-color: #8C1515;
        }

        .inputBox label {
            position: absolute;
            padding: 0 3px;
            left: 60px; /* 调整标签位置，避开图标 */
            top: 25px;
            color: #8C1515;
            font-size: 14px;
            background: #fff;
            opacity: 0;
            transition: 0.5s;
        }

        .inputBox input:focus+label {
            opacity: 1;
            top: -8px;
        }

        .submitButton {
            display: flex;
            align-items: center;
            margin-top: 15px; /* 减少按钮顶部间距 */
        }

        .submitButton input {
            margin: 0 10%;
            flex-grow: 1;
            cursor: pointer;
            height: 45px;
            line-height: 45px;
            background-color: #8C1515;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            outline: none;
        }

        .submitButton input:hover {
            background-color: #6D1111;
            box-shadow: 2px 2px 10px #555;
            transition: 0.3s;
        }

        .submitButton input:active {
            box-shadow: none;
            cursor: wait;
        }

        .register-footer {
            text-align: center;
            margin-top: 25px; /* 调整底部链接区域位置 */
            font-size: 14px;
            padding-bottom: 10px; /* 增加底部内边距避免溢出 */
        }

        .register-footer a {
            color: #8C1515;
            text-decoration: none;
        }

        .register-footer a:hover {
            text-decoration: underline;
        }

        @media (max-width: 500px) {
            .register {
                width: 90%;
                min-height: auto; /* 移动端自动高度 */
                padding: 30px 20px;
                margin: 20px auto; /* 移动端使用上下边距代替绝对居中 */
                margin-left: 20px; /* 保持右移20px */
                position: relative;
                top: auto;
                left: auto;
                right: auto;
                bottom: auto;
            }
        }
    </style>
</head>

<body>
<div class="register">
    <h3>文物展览系统 - 注册</h3>

    <form id="registerForm" th:action="@{/user/register}" method="POST">
        <div class="error-message" id="errorMessage"></div>

        <div class="inputBox">
            <i class="fa fa-user"></i>
            <input type="text" id="username" class="name" name="username" autocomplete="off" placeholder="请输入用户名" required>
            <label>用户名</label>
        </div>

        <div class="inputBox">
            <i class="fa fa-lock"></i>
            <input type="password" id="password" class="inputPassword" autocomplete="off" placeholder="请输入密码" name="password" required>
            <label>密码</label>
        </div>

        <div class="inputBox">
            <i class="fa fa-lock"></i>
            <input type="password" id="ensurepassword" class="inputPassword" autocomplete="off" placeholder="再次输入并确认密码" name="ensurepassword" required>
            <label>确认密码</label>
        </div>

        <div class="inputBox">
            <i class="fa fa-phone"></i>
            <input type="tel" id="phone" class="phone" autocomplete="off" placeholder="请输入手机号（选填）" name="phone">
            <label>手机号</label>
        </div>

        <div class="submitButton">
            <input type="submit" value="注册">
        </div>

        <div class="register-footer">
            <span>已有账号？前往<a th:href="@{/t/login}">登录</a></span>
        </div>
    </form>

    <script>
        document.getElementById('registerForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const ensurepassword = document.getElementById('ensurepassword').value;
            const phone = document.getElementById('phone').value;
            const errorMessage = document.getElementById('errorMessage');

            // 验证两次密码是否一致
            if (password !== ensurepassword) {
                errorMessage.textContent = '两次输入的密码不一致';
                return;
            }

            // 发送注册请求
            fetch('/user/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'username=' + encodeURIComponent(username) +
                    '&password=' + encodeURIComponent(password) +
                    '&nickname=' + encodeURIComponent(username) + // 使用用户名作为昵称
                    '&phone=' + encodeURIComponent(phone)
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 注册成功，跳转到登录页面
                        alert('注册成功，请登录');
                        window.location.href = '/t/login';
                    } else {
                        // 注册失败，显示错误信息
                        errorMessage.textContent = data.message;
                    }
                })
                .catch(error => {
                    console.error('注册出错:', error);
                    errorMessage.textContent = '注册失败，请重试';
                });
        });
    </script>
</div>
</body>
</html>